import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class LoginTextInput extends StatefulWidget {

  LoginTextInput({
    this.hintText,
    this.textEditingController,
    this.focusNode,
    this.onChange,
    this.onDeleteTap,
    this.maxLength:11
  });
  final String hintText;
  final TextEditingController textEditingController;
  final FocusNode focusNode;
  final Function onChange;
  final Function onDeleteTap;
  final int maxLength;

  @override
  _LoginTextInputState createState() => _LoginTextInputState();
}

class _LoginTextInputState extends State<LoginTextInput> {

  bool _showDeleteAll = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Expanded(
          child: Stack(
            children: <Widget>[
              Container(
                // height: 30,
                decoration: BoxDecoration(
                    // color: Colors.amber.withOpacity(0.2),
                    border: Border(
                        bottom: BorderSide(color: Color(0xFFE0E0E0))
                    )
                ),
                child: TextField(
                  focusNode: widget.focusNode,
                  cursorColor: Color(0xFFFFAB00),
                  controller: widget.textEditingController,
                  decoration: InputDecoration(
                      isDense: true,
                      contentPadding: EdgeInsets.fromLTRB(5, 0, 0, 10),
                      hintText: '${widget.hintText}',
                      hintStyle: TextStyle(
                        fontSize: 16,
                        color: Color(0xFF999999),
                        height: 1.3
                      ),
                      border: OutlineInputBorder(
                          borderSide: BorderSide.none
                      )
                  ),
                  inputFormatters: [
                    WhitelistingTextInputFormatter(RegExp("[0-9]")),
                    LengthLimitingTextInputFormatter(widget.maxLength)
                  ],
                  // maxLength: 6,
                  // maxLines: 1,
                  onChanged: (value){
                    _deleteShow(value);
                    if(widget.onChange != null){
                      widget.onChange(value);
                    }
                  },
                ),
              ),
              Positioned(
                right: 10,
                top: 0,
                bottom: 0,
                child: Center(
                  child: _showDeleteAll ? GestureDetector(
                    onTap: (){
                      widget.textEditingController.clear();
                      if(widget.onDeleteTap != null){
                        widget.onDeleteTap();
                      }
                    },
                    child: Icon(Icons.close_rounded, size: 20),
                  ) : Container(),
                ),
              )
            ],
          ),
        )
      ],
    );
  }

  _deleteShow(String value){
    if(value.length > 0){
      setState(() {
        _showDeleteAll = true;
      });
    }else {
      setState(() {
        _showDeleteAll = false;
      });
    }
  }
}
